package com.changgou.system.controller;

import cn.hutool.core.lang.UUID;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.system.pojo.Admin;
import com.changgou.system.service.AdminService;
import com.changgou.system.util.JwtUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {


    @Resource
    private AdminService adminService;

    /***
     * 新增数据
     * @param admin
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Admin admin) {
        if (admin == null) {
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
        this.adminService.add(admin);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    @PostMapping("/login")
    public Result login(@RequestBody Admin admin) {
        boolean login = this.adminService.login(admin);
        if (login) {
            Map<String, String> info = new HashMap<>();
            info.put("username", admin.getLoginName());
            String token =JwtUtil.createJWT(UUID.randomUUID().toString(), admin.getLoginName(),
                            null);
            info.put("token", token);
            return new Result(true, StatusCode.OK, "登录成功", info);
        } else {
            return new Result(false, StatusCode.LOGINERROR, "用户名或者 密码错误");
        }
    }


    /**
     * 查询全部数据
     *
     * @return
     */
    @GetMapping
    public Result findAll() {
//        List<Admin> adminList = adminService.findAll();
//        return new Result(true, StatusCode.OK,"查询成功",adminList) ;

        return null;
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Admin admin = adminService.getById(id);
        return new Result(true, StatusCode.OK, "查询成功", admin);
    }


    /***
     * 修改数据
     * @param admin
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Admin admin, @PathVariable Integer id) {
        admin.setId(id);
        adminService.updateById(admin);
        return new Result(true, StatusCode.OK, "修改成功");
    }


    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Integer id) {
        adminService.removeById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search")
    public Result findList(@RequestParam Map searchMap) {
//        List<Admin> list = adminService.findList(searchMap);
//        return new Result(true,StatusCode.OK,"查询成功",list);
        return null;
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
//    @GetMapping(value = "/search/{page}/{size}" )
//    public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
//        Page<Admin> pageList = adminService.findPage(searchMap, page, size);
//        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
//        return new Result(true,StatusCode.OK,"查询成功",pageResult);
//    }


}
